#
# Copyright 2008-2010 Vadim Zhukov <persgray@gmail.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY AGAVA COMPANY ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AGAVA COMPANY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of the AGAVA Company.

package RB::Archive;

use bytes;
use strict;

use RB::Log;

sub new($$) {
	my $class = shift;
	my $proto = ref($class) || $class;
	my $params = shift;

	# Those three cases indicates problems in the program flow
	die '$params is not a hashref'
	    unless ref($params) eq 'HASH';
	die 'Base path not provided'
	    unless $params->{'basePath'} ne '';
	die 'Date not provided'
	    unless $params->{'date'} ne '';

# 	# Non-critical (can be caused by external reasons) errors follow
# 	unless (exists $params->{'type'}) {
# 		logIt(0, 'Archive type was not provided for '.
# 		    $params->{'basePath'});
# 	}
# 	unless (exists $params->{'format'}) {
# 		logIt(0, 'Archive format was not provided for '.
# 		    $params->{'basePath'});
# 	}
# 	unless (exists $params->{'compressed'}) {
# 		logIt(0, 'Compression flag was not provided for '.
# 		    $params->{'basePath'});
# 	}
# 	unless (exists $params->{'extension'}) {
# 		logIt(0, 'Archive extension was not provided for '.
# 		    $params->{'basePath'});
# 	}
# 	unless (exists $params->{'exit_code'}) {
# 		logIt(0, 'Exit code of program created the archive was not '.
# 		    'provided for'.$params->{'basePath'});
# 	}
# 	unless (exists $params->{'begin'}) {
# 		logIt(0, 'Archive creation started date was not provided for '.
# 		    $params->{'basePath'});
# 	}
# 	unless (exists $params->{'end'}) {
# 		logIt(0, 'Archive creation ended date was not provided for '.
# 		    $params->{'basePath'});
# 	}

	# Next code block borrowed from RB::Profile and should be kept in sync.
	my $c = $params->{'compressed'};
	if ($c eq 'false' or $c eq 'no' or $c eq 'off' or $c eq 'disabled') {
		$c = 0;
	} elsif ($c eq 'true' or $c eq 'yes' or $c eq 'on' or $c eq 'enabled') {
		$c = '+';
	}
	$params->{'compressed'} = $c;

	my $this = { %{$params} };
	bless($params, $proto);
}

sub date($) {
	my $this = shift;
	return $this->{'date'};
}
sub basePath($) {
	my $this = shift;
	return $this->{'basePath'};
}
sub archivePath($) {
	my $this = shift;
	return $this->{'basePath'}.$this->{'extension'};
}
sub infoPath($) {
	my $this = shift;
	return $this->{'basePath'}.'.info';
}

sub beganAt($) {
	my $this = shift;
	return $this->{'begin'};
}
sub endedAt($) {
	my $this = shift;
	return $this->{'end'};
}
sub exitCode($) {
	my $this = shift;
	return $this->{'exit_code'};
}
sub type($) {
	my $this = shift;
	return $this->{'type'};
}
sub format($) {
	my $this = shift;
	return $this->{'format'};
}
sub compressed($) {
	my $this = shift;
	return $this->{'compressed'};
}
sub extension($) {
	my $this = shift;
	return $this->{'extension'};
}
sub increment_from($) {
	my $this = shift;
	return $this->{'increment_from'};
}

sub dumpedOK($) {
	my $this = shift;
	return (exists $this->{'exit_code'}
	    and        $this->{'exit_code'} == 0
# 	    and exists $this->{'begin'}
# 	    and exists $this->{'end'}
# 	    and exists $this->{'type'}
# 	    and exists $this->{'format'}
# 	    and exists $this->{'compressed'}
# 	    and exists $this->{'extension'}
	    );
}

1;
